<?php

namespace App\Http\Controllers\BK;

use App\Http\Controllers\Controller;
use App\Http\Requests\BK\Admin\AdminIndexRequest;
use App\Http\Requests\BK\Admin\AdminStoreRequest;
use App\Http\Requests\BK\Admin\AdminUpdateRequest;
use App\Http\Requests\BK\Admin\RefreshGoogleSecretRequest;
use App\Http\Requests\BK\Admin\UpdatePasswordRequest;
use App\Services\AdminService;
use Illuminate\Http\Request;

class AdminController extends Controller
{
    public function __construct()
    {
        $this->middleware(['auth:admins']);
    }
    /**
     * 用户信息和用户菜单
     * @param Request $request
     * @param AdminService $adminService
     * @return \Illuminate\Http\JsonResponse
     */
    public function getAdminInfo(Request $request, AdminService $adminService)
    {
        return success($adminService->getAdminInfo($request->user()));
    }

    /**
     * 后台用户列表
     *
     * @param AdminIndexRequest $request
     * @param AdminService $adminService
     * @return \Illuminate\Http\JsonResponse
     */
    public function index(AdminIndexRequest $request, AdminService $adminService)
    {
        return success($adminService->getIndex($request->all()));
    }

    /**
     * 后台用户信息
     *
     * @param int $id
     * @param AdminService $adminService
     * @return \Illuminate\Http\JsonResponse
     */
    public function show(int $id,AdminService $adminService)
    {
        return success($adminService->showAdmin($id));
    }

    /**
     * 添加用户
     * @param AdminStoreRequest $request
     * @param AdminService $adminService
     * @return \Illuminate\Http\JsonResponse
     * @throws \App\Exceptions\GeneralException
     */
    public function store(AdminStoreRequest $request, AdminService $adminService)
    {
        return success($adminService->storeAdmin($request->all()));
    }

    /**
     * 修改用户
     * @param AdminUpdateRequest $request
     * @param AdminService $adminService
     * @return \Illuminate\Http\JsonResponse
     * @throws \App\Exceptions\GeneralException
     */
    public function update(AdminUpdateRequest $request,AdminService $adminService)
    {
        return success($adminService->updateAdmin($request->all()));
    }

    /**
     * Description:后台用户信息
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function adminInfo(Request $request)
    {
        $admin = $request->user();
        $res = [
            'google_secret'=>$admin->google_secret,
            'account'=>$admin->account,
        ];
        return success($res);
    }

    /**
     * Description:更新谷歌验证码
     * @param RefreshGoogleSecretRequest $request
     * @param AdminService $adminService
     * @return \Illuminate\Http\JsonResponse
     * @throws \App\Exceptions\GeneralException
     */
    public function refreshGoogleSecret(RefreshGoogleSecretRequest $request,AdminService $adminService)
    {
        return success($adminService->refreshGoogleSecret($request->user(),$request->get('code',null)));
    }

    /**
     * Description:更新密码
     * @param UpdatePasswordRequest $request
     * @param AdminService $adminService
     * @return \Illuminate\Http\JsonResponse
     * @throws \App\Exceptions\GeneralException
     */
    public function updatePassword(UpdatePasswordRequest $request,AdminService $adminService)
    {
        return success($adminService->updatePassword($request->user(),$request->validated()));
    }
}
